package org.example.struct.array2;

/**
 * @author liyishan
 * @date 2024/8/2 10:49
 * @apiNote
 */

public class Test03128NumberOfRightTriangles {
    public static void main(String[] args) {
        int[][] grid = {{1, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}};
        System.out.println(numberOfRightTriangles(grid));
    }

    public static long numberOfRightTriangles(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int[] row = new int[m];
        for (int i = 0; i < m; i++) {
            int count = 0;
            for (int j = 0; j < n; j++) {
                count += grid[i][j];
            }
            row[i] = count;
        }
        int[] col = new int[n];
        for (int i = 0; i < n; i++) {
            int count = 0;
            for (int j = 0; j < m; j++) {
                count += grid[j][i];
            }
            col[i] = count;
        }
        long count = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int cur = grid[i][j];
                if (cur == 1) {
                    count += (row[i] - 1) * (col[j] - 1);
                }
            }
        }
        return count;
    }

}
